package com.hspedu.homework;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * @author LiuJinTao
 * @className HomeWork01
 * @date 2024/06/19 23:54
 * @description TODO
 */
public class HomeWork01 {
    public static void main(String[] args) {

    }

    @Test
    public void testList() {
        DAO<User> userDAO = new DAO<>();
        userDAO.save("1", new User(1, 12, "zhangsan"));
        userDAO.save("2", new User(2, 13, "lisi"));

        User u = userDAO.get("1");
        System.out.println(u);

        userDAO.update("2", new User(2, 14, "lisi"));

        List<User> list = userDAO.list();
        System.out.println(list);

        userDAO.delete("1");
        System.out.println(userDAO);
    }
}

class User {
    private int id;
    private int age;
    private String name;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public User(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class DAO<T> {
    Map<String, T> map = new HashMap<>();

    public void save(String id, T entity) {
        map.put(id, entity);
    }

    public T get(String id) {
        return map.get(id);
    }

    public void update(String id, T entity) {
        map.put(id, entity);
    }

    public List<T> list() {
        ArrayList<T> ts = new ArrayList<>();
        Set<String> strings = map.keySet();
        for (String k : strings) {
            ts.add(get(k));
        }
        return ts;
    }

    public void delete(String id) {
        map.remove(id);
    }

    @Override
    public String toString() {
        return "DAO{" +
                "map=" + map +
                '}';
    }
}